package com.example.figurehecheng.controller.merchant;


import com.example.figurehecheng.common.result.Result;
import com.example.figurehecheng.common.utils.AliOssUtil;
import com.example.figurehecheng.domain.dto.ImageDTO;
import com.example.figurehecheng.domain.dto.SceneryUpdateDTO;
import com.example.figurehecheng.domain.vo.ImageVO;
import com.example.figurehecheng.service.IImageService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 图片关联表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@RestController("adminImageController")
@RequestMapping("/image")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
public class ImageController {

    private final AliOssUtil aliOssUtil;
    private final IImageService imageService;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            log.error("上传的文件为空");
//          return Result.error("上传的文件为空");
        }
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + suffix;
        System.out.println("fileName" + fileName);
        String uploadResult = aliOssUtil.upload(file.getBytes(), fileName);
        System.out.println("uploadResult" + uploadResult);
        return Result.success(uploadResult);
    }

    @PostMapping("/save")
    @ApiOperation("保存图片信息")
    public Result<ImageVO> saveImage(@RequestBody ImageDTO imageDTO) {

        ImageVO imageVO = imageService.saveImage(imageDTO);

        return Result.success(imageVO);
    }


    @DeleteMapping("/delete")
    @ApiOperation("修改景点信息")
    public Result<Boolean> deleteImage(@RequestBody @Valid ImageDTO dto) {
        boolean success = imageService.deleteImage(dto);
        return Result.success(success);
    }

}
